import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'Person.dart';

void main() {
  var onError = FlutterError.onError;
  FlutterError.onError = (FlutterErrorDetails details) {
    onError?.call(details);
    // ... 处理上报逻辑，此处是flutter自己捕获的异常
  };

  runZoned(() => runApp(const MyApp()),
      // 异常配置
      zoneSpecification: ZoneSpecification(
        // 捕获打印
        print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
          // ...收集日志
          // ...
          // 打印 此处可判断是否是debug模式下，如果是再打印
          parent.// print(zone, "Interceptor: $line");
        },
        // 捕获未处理的异步异常
        handleUncaughtError: (self, parent, zone, error, stackTrace) {
          parent.// print(zone, '${error.toString()} $stackTrace');
          // 处理异常
          // 。。。
        },
      ));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<Person>(
      create: (context) => Person(),
      child: MyTheme(
        color: Colors.black,
        child: MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(
                seedColor: const Color.fromARGB(255, 66, 157, 218)),
            useMaterial3: true,
          ),
          home: const MyHomePage(title: 'APP Name'),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Color _color = Colors.black;
  @override
  Widget build(BuildContext context) {
    // print("_MyHomePageState");
    return MyTheme(
      color: _color,
      child: Scaffold(
          appBar: AppBar(title: Text('nihao')),
          body: const Foo(),
          floatingActionButton: Consumer<Person>(
            builder: (context, person, child) {
              // print(child);
              return FloatingActionButton(
                onPressed: () {
                  // print("Consumer name");
                  person.name = "change name";
                },
                child: const Icon(Icons.color_lens),
              );
            },
          )),
    );
  }
}

class MyTheme extends InheritedWidget {
  MyTheme({super.key, required this.color, required super.child});
  Color color;

  @override
  bool updateShouldNotify(covariant MyTheme oldWidget) {
    // print("${oldWidget.color}->>$color");
    // return true;
    return oldWidget.color != color;
  }

  static of(BuildContext context) {
    // print("of");
    return context.dependOnInheritedWidgetOfExactType<MyTheme>()!;
  }
}

class Foo extends StatelessWidget {
  const Foo({super.key});

  @override
  Widget build(BuildContext context) {
    // print('foo build');
    return Column(
      children: [
        Selector<Person, String>(
          selector: (context, person) => person.habby,
          builder: (context, habby, child) {
            // print("habby");
            return Text(
              habby,
              style: const TextStyle(fontSize: 40),
            );
          },
        ),
        Selector<Person, String>(
          selector: (context, person) => person.name,
          builder: (context, name, child) {
            // print('name');
            return Text(
              name,
              style: TextStyle(fontSize: 30, color: MyTheme.of(context).color),
            );
          },
        ),
      ],
    );
  }
}
